/**
 * Copyright (c) Software Institude of PKU
 * All rights reserved. 
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package edu.pku.sei.modeler.jet.util;

import java.util.ArrayList;
import java.util.List;

import edu.pku.sei.modeler.model.core.MetaClass;
import edu.pku.sei.modeler.model.core.MetaElement;
import edu.pku.sei.modeler.model.core.MetaEnum;
import edu.pku.sei.modeler.model.core.MetaModel;
import edu.pku.sei.modeler.model.core.MetaPackage;
import edu.pku.sei.modeler.model.core.MetaTypedElement;

/**
 * @author zl
 *
 */
public class PackageRetriever {
	public static List pkgPathList;
	static {
		pkgPathList = new ArrayList();
	}
	public static void init(MetaModel model) {
		pkgPathList.clear();
		String base = ModelerConstants.pkgBase+"."+ModelerConstants.modelBase;
		List pkgs = model.getPackages();
		for(int i=0; i<pkgs.size(); i++) {
			retrievePkgPath(base, (MetaPackage)pkgs.get(i));
		}
	}
	private static void retrievePkgPath(String base, MetaPackage pkg) {
		base = base + "." + pkg.getName();
		pkgPathList.add(base);
		List pkgs = pkg.getSubPackages();
		for(int i=0; i<pkgs.size(); i++) {
			retrievePkgPath(base, (MetaPackage)pkgs.get(i));
		}
	}
	
	public static String retrievePackage(MetaClass cls) {
		String result = "";
		MetaElement container = cls.getContainer();
		while(container instanceof MetaPackage) {
			result = ((MetaPackage)container).getName()+"."+result;
			container = container.getContainer();
		}
		if(result.endsWith(".")) {
			result = result.substring(0, result.length()-1);
		}
		result = ModelerConstants.pkgBase + "."
			+ ModelerConstants.modelBase + "." + result;
		return result;
	}
	public static String retrievePackage(MetaEnum enu) {
		String result = "";
		MetaElement container = enu.getContainer();
		while(container instanceof MetaPackage) {
			result = ((MetaPackage)container).getName()+"."+result;
			container = container.getContainer();
		}
		if(result.endsWith(".")) {
			result = result.substring(0, result.length()-1);
		}
		result = ModelerConstants.pkgBase + "."
			+ ModelerConstants.modelBase + "." + result;
		return result;
	}
	public static String retrievePkgClassName(MetaClass cls) {
		String pkgStr = "";
		if(cls.getContainer() instanceof MetaPackage) {
			pkgStr = ((MetaPackage)cls.getContainer()).getName();
		}
		if("".equals(pkgStr)) {
			return "Default";
		}
		String head = pkgStr.toUpperCase().substring(0, 1);
		pkgStr = pkgStr.substring(1);
		pkgStr = head+pkgStr;
		return pkgStr;
	}
	
	public static String retrieveAttributeID(MetaClass cls, String attrName) {
		String result = "ModelPackage" + "." + cls.getName().toUpperCase()+"_"+attrName.toUpperCase();
		return result;
	}
	
	public static String retrieveClassID(MetaClass cls) {
//		String result = "ModelPackage" + ".__" + cls.getFullName().replace(".", "_").toUpperCase()+"__";
		String result = "ModelPackage" + ".__" + cls.getName().toUpperCase()+"__";
		return result;
	}
	
	public static List<String> retrieveClassImports(MetaClass cls) {
		List<String> imports = new ArrayList<String>();
		List<MetaTypedElement> elements = new ArrayList<MetaTypedElement>();
		elements.addAll(cls.getAttributes());
		elements.addAll(cls.getDerivedAttributes());
		elements.addAll(cls.getReferences());
		elements.addAll(cls.getDerivedReferences());
		for(MetaTypedElement tEle:elements) {
			if(tEle.getType() instanceof MetaClass) {
				MetaClass impt = (MetaClass)tEle.getType();
				if(impt.getContainer() != cls.getContainer()) {
					imports.add(retrievePackage(impt)+"."+impt.getName());
				}
			}
			else if(tEle.getType() instanceof MetaEnum) {
				MetaEnum impt = (MetaEnum)tEle.getType();
				if(impt.getContainer() != cls.getContainer()) {
					imports.add(retrievePackage(impt)+"."+impt.getName());
				}
			}
		}
		return imports;
	}
}
